package com.simple.system.controller;

import com.simple.system.entity.Result;
import com.simple.system.entity.SysRoles;
import com.simple.system.entity.SysUser;
import com.simple.system.service.SysOptLogService;
import com.simple.system.service.SysRolesService;
import com.simple.system.service.SysUserService;
import com.simple.system.util.CurrentUserUtil;
import com.simple.system.util.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/roles")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SysRolesController {
    @Autowired
    private SysRolesService rolesService;
    @Autowired
    private SysOptLogService operationLogService;

    @GetMapping("/list")
    public Result<List<SysRoles>> list(HttpServletRequest request) {
        List<SysRoles> roles = rolesService.getAllRoles();
        Long userId = CurrentUserUtil.getCurrentUserId(request);
        operationLogService.saveLog(userId, "LIST", "role list query");
        return Result.success(roles);

    }

    @PostMapping("/save")
    public Result<SysRoles> save(@RequestBody SysRoles role) {
        SysRoles savedRole = rolesService.saveRole(role);
        operationLogService.saveLog(role.getId(), "SAVE", "role saved: " + role.getName());

        return Result.success(savedRole);
    }

    @DeleteMapping("/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        rolesService.deleteRole(id);
        operationLogService.saveLog(id, "DELETE", "Role deleted: " + id);
        return Result.success("删除成功");
    }
}    